package wubj.com.tendencydemo.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import java.util.Map;

/**
 * Wubj 创建于 2017/5/16.
 */
public class MyBaseActivity extends AppCompatActivity implements DialogControl {

    protected Context mContext;
    protected Context context;
    protected ProgressDialog mProgress;
//    protected OkHttpClient client;//网络请求的client

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 添加Activity到堆栈
//        AppManager.getAppManager().addActivity(this);
        mContext = this;
        context = this;
        mProgress = getWaitDialog(mContext, "正在加载中...");
//        mProgress = SuccinctProgress.getSuccinctProgress(mContext, getString(R.string.loading));
        mProgress.setCancelable(true);
        mProgress.setCanceledOnTouchOutside(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 结束Activity&从堆栈中移除
//        AppManager.getAppManager().finishActivity(this);
//        if (EventBus.getDefault().isRegistered(this)) {
//            EventBus.getDefault().unregister(this);
//        }
    }

    protected void getIntentData(Intent intent) {
    }

    /**
     * 初始化视图
     */
    public void initView() {
    }

    public void initData() {
//        client = new OkHttpClient();
    }

    public void initListener() {
    }

//    /**
//     * @param url
//     * @param formBody
//     */
//    protected String initHttp(String url, FormBody formBody) {
//        Request request = new Request.Builder()
//                .url(url)
//                .post(formBody)
//                .build();
//        LogUtils.e("wangtao---->>>:网路请求地址:" + url);
//        try {
//            Response response = client.newCall(request).execute();
//            if (response.isSuccessful()) {
//                return response.body().string();
//            } else {
//                return "请求失败";
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return "请求失败";
//    }

    public void onEventMainThread(Map<String, Object> inParam) {
    }

    public void showProgress(String text) {
        if (mProgress != null) {
            if (!mProgress.isShowing()) {
                if (!TextUtils.isEmpty(text)) {
                    setMessage(text);
                }
                mProgress.show();
            } else {
                if (!TextUtils.isEmpty(text)) {
                    setMessage(text);
                }
            }
        }
    }

    private void setMessage(String msg) {
        mProgress.setMessage(msg);
    }

    /**
     * 不显示圈圈
     */
    private void dismissProgress() {
        if (mProgress != null && mProgress.isShowing()) {
            mProgress.dismiss();
        }
    }

    @Override
    public void hideWaitDialog() {
        dismissProgress();
//        SuccinctProgress.dismiss();
    }

    @Override
    public void showWaitDialog() {
        showProgress("正在加载中...");
//        SuccinctProgress.showSuccinctProgress(mContext, getString(R.string.loading));
    }

    @Override
    public void showWaitDialog(int resourcesId) {
        showProgress(getString(resourcesId));
//        SuccinctProgress.showSuccinctProgress(mContext, getString(resourcesId));
    }

    @Override
    public void showWaitDialog(String text) {
        showProgress(text);
//        SuccinctProgress.showSuccinctProgress(mContext, text);
    }

    public ProgressDialog getWaitDialog(Context context, String message) {
        ProgressDialog waitDialog = new ProgressDialog(context);
        if (!TextUtils.isEmpty(message)) {
            waitDialog.setMessage(message);
        }
        return waitDialog;
    }

//    public OkHttpClient getClient() {
//        return client;
//    }
}
